package com.example.autopia.view.Activities

import android.R
import android.app.Dialog
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.util.Patterns
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ImageButton
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.autopia.databinding.ActivityCreateCarBinding
import com.example.autopia.model.Item.Car
import com.example.autopia.model.Repositories.UsersRepository
import com.example.autopia.utilities.PreferenceManager
import com.example.autopia.view.Adapters.ColorItemAdapter
import com.example.autopia.view.Listeners.ColorItemListener
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase


class CreateCarActivity : AppCompatActivity() {
    private lateinit var binding: ActivityCreateCarBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCreateCarBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.ibCreateCar.setOnClickListener {
            if (validateData( binding.etModel.text.toString(), binding.etNumber.text.toString())) {
                val userCar = Car(
                    binding.etModel.text.toString(), binding.etNumber.text.toString(), getCodeColor(binding.tvColor.text.toString())
                )
                UsersRepository().addCar(Firebase.auth.currentUser!!.uid, userCar)
                val createCarPreference: PreferenceManager = PreferenceManager(this@CreateCarActivity)
                createCarPreference.putBoolean("createCar", true)
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                finish()
            }

        }

        binding.bMiss.setOnClickListener {
            val createCarPreference: PreferenceManager = PreferenceManager(this@CreateCarActivity)
            createCarPreference.putBoolean("createCar", true)
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }

        binding.tvColor.setOnClickListener {
            showDialog()
        }

    }
    private fun showDialog() {
        val dialog = Dialog(this@CreateCarActivity)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(com.example.autopia.R.layout.dialog_choise_color)

        val rec = dialog.findViewById<RecyclerView>(com.example.autopia.R.id.ColorList)
        val btClose = dialog.findViewById<ImageButton>(com.example.autopia.R.id.btClose)

        val list = ArrayList<String>()
        list.add("Чёрный")
        list.add("Красный")
        list.add("Оранжевый")
        list.add("Жёлтый")
        list.add("Зелёный")
        list.add("Голубой")
        list.add("Синий")
        list.add("Фиолетовый")
        list.add("Коричневый")
        list.add("Серый")
        list.add("Белый")
        list.add("Розовый")
        val adapter = ColorItemAdapter()
        rec.adapter = adapter
        adapter.setData(list)

        adapter.attachListener(object : ColorItemListener{
            override fun onColorItemClicked(color: String) {
                dialog.dismiss()
                binding.tvColor.text = color
            }
        })

        btClose.setOnClickListener {
            dialog.dismiss()
        }
        dialog.show()
        dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.window?.attributes!!.windowAnimations = com.example.autopia.R.style.DialogAnimation
        dialog.window?.setGravity(Gravity.BOTTOM)
    }
    private fun getCodeColor(color:String) : String{
        return when(color){
            "Чёрный"-> "black"
            "Красный"-> "red"
            "Оранжевый"-> "orange"
            "Жёлтый"-> "yellow"
            "Зелёный"-> "green"
            "Голубой"-> "blue_light"
            "Синий"-> "blue"
            "Фиолетовый"-> "purple"
            "Розовый"-> "pink"
            "Коричневый"-> "brown"
            "Серый"-> "grey"
            "Белый"-> "white"
            else-> "black"
        }
    }

    private fun validateData(model: String, number: String): Boolean {
        if (model.length < 1) {
            binding.etModel.error = "Некорректный ввод"
            return false
        }
        if (number.length < 5) {
            binding.etNumber.error = "Длина должна быть больше четырех символов"
            return false
        }
        return true
    }
}